//
//  newDrop.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/5/24.
//  Copyright © 2016年 zz. All rights reserved.
//

#define CELLHEIGHT 44
#define CELLWITH 130



#import "newDrop.h"
#import "DropCell.h"

static newDrop * dropDownObj = nil;

@interface newDrop()<UITableViewDelegate,UITableViewDataSource>
{
    
    CGRect btn;
    NSArray * cellContents;
}

@property(nonatomic, strong) UITableView *table;
@end

@implementation newDrop

-(void)setDropDownMenuParameterWithcellContents:(NSArray *)contentsArray{
    
    cellContents = contentsArray;
    [self setUpList];
}

-(void)setDropDownMenuParameterWithcellContents:(NSArray *)contentsArray andWidth:(CGFloat)dropViewWithd{
    
    cellContents = contentsArray;
    
    [_table reloadData];
//    [self setUpList];
}

#pragma mark -单例方法
+ (newDrop *)shareDropDownMenu
{
    if (dropDownObj == nil)
    {
        dropDownObj = [[self alloc] init];
    }
    return dropDownObj;
}

- (id)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)setUpList
{
    self.frame = CGRectMake(btn.origin.x, btn.origin.y + btn.size.height,CELLWITH, 0);
    self.backgroundColor = [UIColor blackColor];
    self.layer.masksToBounds = NO;
    self.layer.cornerRadius = 8;
    //self.layer.shadowOffset = CGSizeMake(-5, 5);
    //self.layer.shadowRadius = 5;
    //self.layer.shadowOpacity = 0.5;
    
//    UIView *view = [[UIView alloc] init];
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"homework_down_"]];
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CELLWITH, 0)];
    _table.delegate = self;
    _table.dataSource = self;
    
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.backgroundColor=[UIColor clearColor];
    _table.showsVerticalScrollIndicator = NO;
    [self addSubview:_table];
    
}
- (void)hidden{
    if (_isDisplay)
    {
        _isDisplay = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.0f];
        self.frame = CGRectMake(self.frame.origin.x ,self.frame.origin.y, self.frame.size.width, 0);
        _table.frame = CGRectMake(0 ,0, _table.frame.size.width, 0);
        [UIView commitAnimations];
    }
}

-(void)hideDropDown:(UIView *)b
{
    NSObject *objec = (NSObject *)_delegate;
    if (b!= nil)
    {
        
        if ([objec isKindOfClass:[UIViewController class]]) {
            UIViewController * controller = (UIViewController *)_delegate;
            btn  = [b convertRect:b.bounds toView:controller.view];
            
            //CGFloat x = btn.origin.x;
            //x += 10;
            //btn = CGRectMake(x, btn.origin.y, btn.size.width, btn.size.height);
        }
        else if ([objec isKindOfClass:[UIView class]]){
            btn  = [b convertRect:b.bounds toView:(UIView *)_delegate];
        }
        
    }
    
    NSInteger droplistX;
    if (btn.origin.x + btn.size.width/2 < SCREENWIDTH/2) {
        
        if (btn.origin.x + self.frame.size.width < SCREENWIDTH)
        {
            droplistX = btn.origin.x;
        }
        else
        {
            droplistX = btn.origin.x - (SCREENWIDTH - (btn.origin.x + self.frame.size.width));
        }
    }
    else
    {
        if (btn.origin.x + btn.size.width > self.frame.size.width)
        {
            droplistX = btn.origin.x + btn.size.width - self.frame.size.width;
        }
        else
        {
            droplistX = 0;
        }
    }
    
    
    if (_isDisplay)
    {
        _isDisplay = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        self.frame = CGRectMake(droplistX ,btn.origin.y + btn.size.height, self.frame.size.width, 0);
        _table.frame = CGRectMake(0 ,0, self.frame.size.width, 0);
        [UIView commitAnimations];
        
        if ([self.delegate respondsToSelector:@selector(packUpListDelegate)]) {
            
            [self.delegate packUpListDelegate];
        }

    }
    else
    {
        self.frame = CGRectMake(droplistX ,btn.origin.y + btn.size.height, self.frame.size.width, 0);
        _isDisplay = YES;
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        
        if (CELLHEIGHT * [cellContents count] > 190) {
        self.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height,self.frame.size.width, 190);
        _table.frame = CGRectMake(0, 10, self.frame.size.width, 190);
        }
        else
        {
        
        self.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height,self.frame.size.width, CELLHEIGHT*[cellContents count]);
        _table.frame = CGRectMake(0, 10, self.frame.size.width, CELLHEIGHT*[cellContents count]);
        
        /*
         self.frame = CGRectMake(SCREENWIDTH - self.frame.size.width - 10, CGRectGetMaxY(btn), SCREENWIDTH - droplistX - 10, CELLHEIGHT * [cellContents count]);
         _table.frame = CGRectMake(0, 10, self.frame.size.width, CELLHEIGHT*[cellContents count]);
         */
        }
        
        [UIView commitAnimations];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELLHEIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cellContents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
    
    DropCell *cell;
    if (tableView  == _table) {
        //cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        //if (cell == nil)
        //{
        cell = [[DropCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //}
    }
    
    
    cell.classLabel.text = [cellContents objectAtIndex:indexPath.row];
    
    cell.backgroundColor = RGBACOLOR(73, 72, 75, 1);
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    /*
     cell.textLabel.font = [UIFont systemFontOfSize:15];
     cell.textLabel.textAlignment = NSTextAlignmentCenter;
     if (indexPath.row < cellContents.count) {
     
     cell.textLabel.text = [cellContents objectAtIndex:indexPath.row];
     }else{
     cell.textLabel.text = @"确定";
     }
     UIView * v = [[UIView alloc] init];
     v.backgroundColor = [UIColor grayColor];
     cell.selectedBackgroundView = v;
     */
    return cell;
}

//选择表格事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    _selectedTitle = [cellContents objectAtIndex:indexPath.row];
    
    //设置tag值，用于代理方法里面传值使用
    self.senderTag = indexPath.row;
    
    //代理方法
    [_delegate niDropDownDelegateMethod:self];
    _isDisplay = YES;
    //收回下拉的tableView表格
    [self packUpList];
    
}


#pragma mark -收回下拉的表格
- (void)packUpList
{
    if (_isDisplay)
    {
        CGRect selfFrame = self.frame;
        _isDisplay = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        self.frame = CGRectMake(selfFrame.origin.x ,selfFrame.origin.y, self.frame.size.width, 0);
        _table.frame = CGRectMake(0 ,0, self.frame.size.width, 0);
        [UIView commitAnimations];
        
        if ([self.delegate respondsToSelector:@selector(packUpListDelegate)]) {
            
            [self.delegate packUpListDelegate];
        }
        
        //[self removeFromSuperview];
    }
}

@end
